import { createStore } from 'vuex';
import UserService from '../services/UserService.js';

export default createStore({
  state: {
    user: {
      id: '',
      name: '',
      email: '',
      accessToken: '',
      role: '',
      roles: [],
    },
  },
  mutations: {
    disconnect(state) {
      state.user.id = '';
      state.user.name = '';
      state.user.role = '';
      state.user.email = '';
      state.user.roles = [];
    },
    connect(state, data) {
      state.user.email = data.email;
      state.user.role = data.role;
      state.user.id = data.id;
      state.user.name = data.name;
      state.user.roles = [data.role];
    },
  },
  actions: {
    async connect(state, data) {
      // here you make an API call to authenticate the user
      const res = await UserService.login(data);
      console.log(res);
      this.commit('connect', {
        'role': res.data.role,
        'email': data.email,
        'id': res.data.id,
        'name': data.name,
      });
    },
  },
  modules: {
  },
});
